//
//  LAGPointData.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2018/1/5.
//  Copyright © 2018年 2Bulu. All rights reserved.
//

#import "LAGPointData.h"
#import <FoundationUtilities/FoundationUtilities.h>

@interface LAGPointData ()
@property (nonatomic) CLLocationCoordinate2D coordinate;
@end

@implementation LAGPointData
{
    NSHashTable *_MKMapAnnotationManagerObserverTable;
}

+ (nullable NSArray<NSString *> *)modelPropertyBlacklist {
    return @[@"gpsCoordinate",@"mapDisplayCoordinate"];
}
-(NSDate *)timestamp{
    return self.timeMark;
}

-(nullable NSDictionary *)coordinateDic {
    if(fabs(self.gpsCoordinate.latitude) < 0.000001
        && fabs(self.coordinate.longitude) < 0.000001 )
        return nil;
    
    return @{@"latitude":@(self.gpsCoordinate.latitude),@"longitude":@(self.gpsCoordinate.longitude)};
}

-(void)getCoordFromDic:(NSDictionary *)cdic {
    if(!cdic)
        return;
    CLLocationCoordinate2D c;
    c.latitude = [[cdic objectForKey:@"latitude"] doubleValue];
    c.longitude = [[cdic objectForKey:@"longitude"] doubleValue];
    self.gpsCoordinate = c;
}

- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic{
    [self getCoordFromDic:[dic objectForKey:@"coordinate"]];
    return YES;
}
- (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic{
    
    NSDictionary *cDic = [self coordinateDic];
    if(cDic)
        [dic setObject:cDic forKey:@"coordinate"];
    return YES;
}

-(BOOL)shouldCustomEncodeValueForKey:(NSString *)propertyKey withCoder:(NSCoder *)aCoder
{
    if([propertyKey isEqualToString:@"coordinate"]){
        
        return YES;
    }
    else if([propertyKey isEqualToString:@"gpsCoordinate"]
            || [propertyKey isEqualToString:@"mapDisplayCoordinate"])
    {
        return YES;
    }
    
    return NO;
}

-(BOOL)shouldCustomDecodeValueForKey:(NSString *)propertyKey withCoder:(NSCoder *)aDecoder
{
    if([propertyKey isEqualToString:@"coordinate"]){
        
        return YES;
    }
    
    return NO;
}


-(void)copyPropertiesFromSourceObject:(LAGPointData *)point
{
    [super copyPropertiesFromSourceObject:point];
    self.gpsCoordinate = point.gpsCoordinate;
    self.coordinate = point.coordinate;
}


-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
//#warning willChangeValueForKey 和 didChangeValueForKey 是不必要手动添加的，而且不能手动添加，否则会导致在某些情况下发生闪退
    // [self willChangeValueForKey:@"coordinate"];
    
    //因为在切换图源的时候，会纠偏然后调用annotaion的setCoordinate方法，所以这里就相当于设置地图显示坐标
    _mapDisplayCoordinate = newCoordinate;
    
    // [self didChangeValueForKey:@"coordinate"];
}

// 因为annotatoinView 会监听 coordinate属性，所以这里要
-(void)setGpsCoordinate:(CLLocationCoordinate2D)gpsCoordinate
{
    _gpsCoordinate = gpsCoordinate;
    
    //给一个默认值
    if(fabs(_mapDisplayCoordinate.latitude)<0.000001 && fabs(_mapDisplayCoordinate.longitude)<0.000001)
        _mapDisplayCoordinate = gpsCoordinate;
}

-(void)setMapDisplayCoordinate:(CLLocationCoordinate2D)mapDisplayCoordinate
{
    // _mapDisplayCoordinate = mapDisplayCoordinate;
    self.coordinate = mapDisplayCoordinate;
}

-(CLLocationCoordinate2D)coordinate
{
    return _mapDisplayCoordinate;
}





-(BOOL)isTheSameToPoint:(LAGPointData *)otherPoint
{
    if([otherPoint isEqual:self])
        return YES;
    
    if(  ((!self.timeMark && !otherPoint.timeMark) || [self.timeMark isEqual:otherPoint.timeMark])
       &&  fabs(self.gpsCoordinate.latitude - otherPoint.gpsCoordinate.latitude) < 0.000000001
       && fabs(self.gpsCoordinate.longitude - otherPoint.gpsCoordinate.longitude) < 0.000000001
       && (fabs(self.altitude - otherPoint.altitude) < 0.000000001)
       && (fabs(self.speed - otherPoint.speed) < 0.000000001))
        return YES;
    
    
    return NO;
}


@end
